#include <iostream>
#include <conio.h>
#include <windows.h>
#include <vector>
#include <ctime>
#include <fstream>
#include <string>

using namespace std;

bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score, highScore;
vector<pair<int, int>> snake;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;

// 从文件读取最高分
void ReadHighScore() {
    ifstream inFile("highscore.txt");
    if (inFile.is_open()) {
        inFile >> highScore;
        inFile.close();
    } else {
        highScore = 0;
    }
}

// 保存最高分到文件
void SaveHighScore() {
    ofstream outFile("highscore.txt");
    if (outFile.is_open()) {
        outFile << highScore;
        outFile.close();
    }
}

// 显示游戏标题和装饰
void ShowTitle() {
    cout << R"(
   _____              __  .__                ___________                      
  /  _  \   ____     |  | |  |   ____   ____ \_   _____/____    _____   ____  
 /  /_\  \ /    \    |  | |  |  /  _ \ /  _ \ |    __) \__  \  /     \_/ __ \ 
/    |    \   |  \   |  |_|  |_(  <_> |  <_> )|     \   / __ \|  Y Y  \  ___/ 
\____|__  /___|  /   |____/____/\____/ \____/ \___  /  (____  /__|_|  /\___  >
        \/     \/                                 \/        \/      \/     \/ 
)" << endl;
}

void Setup() {
    gameOver = false;
    dir = RIGHT; // 初始自动向右移动
    x = width / 2;
    y = height / 2;
    snake.clear();
    snake.push_back({x, y});
    
    srand(time(0));
    fruitX = rand() % width;
    fruitY = rand() % height;
    
    score = 0;
    ReadHighScore();
}

void Draw() {
    system("cls");
    ShowTitle();
    
    // 绘制上边界
    cout << "    ";
    for (int i = 0; i < width + 2; i++)
        cout << "\033[44m \033[0m";
    cout << endl;
    
    for (int i = 0; i < height; i++) {
        cout << "    \033[44m \033[0m"; // 左边界
        
        for (int j = 0; j < width; j++) {
            bool isBody = false;
            for (size_t k = 1; k < snake.size(); k++) {
                if (snake[k].first == j && snake[k].second == i) {
                    cout << "\033[42mo\033[0m";
                    isBody = true;
                    break;
                }
            }
            
            if (!isBody) {
                if (i == y && j == x)
                    cout << "\033[42mO\033[0m"; // 蛇头
                else if (i == fruitY && j == fruitX)
                    cout << "\033[41mF\033[0m"; // 水果
                else
                    cout << " ";
            }
        }
        
        cout << "\033[44m \033[0m" << endl; // 右边界
    }
    
    // 绘制下边界
    cout << "    ";
    for (int i = 0; i < width + 2; i++)
        cout << "\033[44m \033[0m";
    cout << endl;
    
    cout << "\n    Current Score: \033[33m" << score << "\033[0m";
    cout << "    High Score: \033[33m" << highScore << "\033[0m" << endl;
    cout << "\n    Controls: W(Up) A(Left) S(Down) D(Right) X(Exit)" << endl;
}

void Input() {
    if (_kbhit()) {
        switch (_getch()) {
        case 'a':
        case 'A':
            if (dir != RIGHT)
                dir = LEFT;
            break;
        case 'd':
        case 'D':
            if (dir != LEFT)
                dir = RIGHT;
            break;
        case 'w':
        case 'W':
            if (dir != DOWN)
                dir = UP;
            break;
        case 's':
        case 'S':
            if (dir != UP)
                dir = DOWN;
            break;
        case 'x':
        case 'X':
            gameOver = true;
            break;
        }
    }
}

void Logic() {
    pair<int, int> prevTail = snake.back();
    
    // 移动蛇身
    for (size_t i = snake.size() - 1; i > 0; i--) {
        snake[i] = snake[i - 1];
    }
    
    // 根据方向移动蛇头
    switch (dir) {
    case LEFT:
        x--;
        break;
    case RIGHT:
        x++;
        break;
    case UP:
        y--;
        break;
    case DOWN:
        y++;
        break;
    default:
        break;
    }
    
    snake[0] = {x, y};
    
    // 检查是否吃到水果
    if (x == fruitX && y == fruitY) {
        score += 10;
        snake.push_back(prevTail);
        
        // 生成新的水果位置
        fruitX = rand() % width;
        fruitY = rand() % height;
        
        // 确保水果不会出现在蛇身上
        bool onSnake;
        do {
            onSnake = false;
            for (const auto& segment : snake) {
                if (segment.first == fruitX && segment.second == fruitY) {
                    onSnake = true;
                    fruitX = rand() % width;
                    fruitY = 